package com.lanou.connotationjoke.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.android.volley.toolbox.NetworkImageView;
import com.lanou.connotationjoke.R;
import com.lanou.connotationjoke.activity.LoginActivity;
import com.lanou.connotationjoke.adapter.ReviewPagerAdapter;
import com.lanou.connotationjoke.entity.LoginUserEty;
import com.lanou.connotationjoke.tool.VolleySingleton;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

/**
 * Created by dllo on 16/1/8.
 */
public class ReviewFragment extends BaseFragment{
    private SharedPreferences sharedPreferences;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private ReviewPagerAdapter adapter;
    private List<Fragment> datas;
    private NetworkImageView loginIv;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_review,null);
        tabLayout = (TabLayout) view.findViewById(R.id.tablayout_filter);
        viewPager = (ViewPager) view.findViewById(R.id.viewpager_filter);
        loginIv = (NetworkImageView) view.findViewById(R.id.iv_filter_to_login);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        EventBus.getDefault().register(this);

        sharedPreferences = getActivity().getSharedPreferences("userData", Context.MODE_PRIVATE);
        String iconUrl = sharedPreferences.getString("icon","");
        loginIv.setDefaultImageResId(R.mipmap.ic_discovery_default_avatar);
        loginIv.setErrorImageResId(R.mipmap.ic_discovery_default_avatar);
        loginIv.setImageUrl(iconUrl, VolleySingleton.getImageLoader());


        datas = new ArrayList<>();
        datas.add(new ReviewGameFragment());
        datas.add(new ReviewMovieFragment());
        adapter = new ReviewPagerAdapter(getFragmentManager(),datas);
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
        loginIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    @Subscribe(threadMode = ThreadMode.MainThread)
    public void onMainEvent(LoginUserEty loginUserEty) {
        loginIv.setImageUrl(loginUserEty.getIconUrl(), VolleySingleton.getImageLoader());
    }
}
